/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.androidpush.client;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Toast;

import org.androidpush.activity.ChatActivity;
import org.androidpush.utils.ConstantUtils;

import java.util.Random;

/** 
 * This class is to notify the user of messages with NotificationManager.
 *
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class ChatMsger {

    private static final String LOGTAG = LogUtil.makeLogTag(ChatMsger.class);

    private static final Random random = new Random(System.currentTimeMillis());

    private Context context;

    private SharedPreferences sharedPrefs;

    private NotificationManager notificationManager;

    public ChatMsger(Context context) {
        this.context = context;
        this.sharedPrefs = context.getSharedPreferences(
                Constants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
        this.notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
    }

    public void chatmsg(String toUser, String fromUser, String chatDate,
            String chatMessage, String chatType) {
        Log.d(LOGTAG, "ChatMsger()...");

        Log.d(LOGTAG, "toUser=" + toUser);
        Log.d(LOGTAG, "fromUser=" + fromUser);
        Log.d(LOGTAG, "chatDate=" + chatDate);
        Log.d(LOGTAG, "chatMessage=" + chatMessage);
        Log.d(LOGTAG, "chatType=" + chatType);

        /*Intent newIntent = new Intent(Constants.ACTION_SHOW_CHATACTIVITY);
        newIntent.putExtra(Constants.CHATMSG_TOUSER, toUser);
        newIntent.putExtra(Constants.CHATMSG_FROMUSER, fromUser);
        newIntent.putExtra(Constants.CHATMSG_DATE, chatDate);
        newIntent.putExtra(Constants.CHATMSG_MESSAGE, chatMessage);
        newIntent.putExtra(Constants.CHATMSG_TYPE, chatType);
        context.sendBroadcast(newIntent);*/

        if (isNotificationEnabled()) {
            // Show the toast
            if (isNotificationToastEnabled()) {
                Toast.makeText(context, chatMessage, Toast.LENGTH_LONG).show();
            }

            Intent intent = new Intent(context,
                    ChatActivity.class);
            intent.putExtra(ConstantUtils.FRIEND_ID, fromUser);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent contentIntent = PendingIntent.getActivity(context, random.nextInt(),
                    intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Notification.Builder builder = new Notification.Builder(context);
            builder.setAutoCancel(true)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setWhen(System.currentTimeMillis())
                    .setTicker(fromUser)
                    .setSmallIcon(getNotificationIcon())
                    .setContentText(chatMessage)
                    .setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
                    .setContentIntent(contentIntent);

            Notification notification = builder.build();

            if (isNotificationSoundEnabled()) {
                notification.defaults |= Notification.DEFAULT_SOUND;
            }
            if (isNotificationVibrateEnabled()) {
                notification.defaults |= Notification.DEFAULT_VIBRATE;
            }
            notification.flags |= Notification.FLAG_AUTO_CANCEL;
            notification.when = System.currentTimeMillis();
            notification.tickerText = chatMessage;
            notification.icon = getNotificationIcon();
            notification.defaults = Notification.DEFAULT_LIGHTS;

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            /*notification.setLatestEventInfo(context, title, message,
                    contentIntent);*/
            notificationManager.notify(random.nextInt(), notification);

        } else {
            Log.w(LOGTAG, "ChatMsg disabled.");
        }
    }

    private int getNotificationIcon() {
        return sharedPrefs.getInt(Constants.NOTIFICATION_ICON, 0);
    }

    private boolean isNotificationEnabled() {
        return sharedPrefs.getBoolean(Constants.SETTINGS_NOTIFICATION_ENABLED,
                true);
    }

    private boolean isNotificationSoundEnabled() {
        return sharedPrefs.getBoolean(Constants.SETTINGS_SOUND_ENABLED, true);
    }

    private boolean isNotificationVibrateEnabled() {
        return sharedPrefs.getBoolean(Constants.SETTINGS_VIBRATE_ENABLED, true);
    }

    private boolean isNotificationToastEnabled() {
        return sharedPrefs.getBoolean(Constants.SETTINGS_TOAST_ENABLED, false);
    }

}
